package com.zx.idc.backend.gui.report.common;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseDependencyInjectionApi;
import com.zx.idc.ds.common.entity.BaseEntity;
import com.zx.idc.ds.common.service.BaseService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joor.Reflect;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.util.List;

import static com.zx.idc.backend.gui.common.ResultBuilder.success;

/**
 * @author rxliuli
 */
public interface BaseWeekApi<Service extends BaseService<Entity>, Entity extends BaseEntity, Vo extends Entity> extends BaseDependencyInjectionApi<Service, Entity> {
    /**
     * 报表信息
     *
     * @param vo vo 参数
     * @return 数据列表
     */
    @GetMapping("/report")
    @ResponseBody
    @RequiresPermissions(value = {"report-statistics:r-domain-archive", "report-statistics:r-illegal-info", "report-statistics:r-illegal-media", "report-statistics:r-shield", "report-statistics:t-log-security", "report-statistics:t-log-operation"}, logical = Logical.OR)
    default Result<List<Entity>> report(Vo vo) {
        final LocalDate dateNow = Reflect.on(vo).get("dateNow");
        return success(using(BaseWeekApi.class, service -> service.selectList(new EntityWrapper<Entity>(vo)
            .eq("belongYear", dateNow.getYear())
            .le("week", ChronoField.ALIGNED_WEEK_OF_YEAR.getFrom(dateNow))
        )));
    }
}
